package com.export.service;

import com.alibaba.fastjson.JSON;
import com.export.dao.AccountReportDao;
import com.export.dao.OperateLogDao;
import com.export.entity.AccountReport;
import com.export.entity.AccountReportDetail;
import com.export.entity.DownloadLog;
import com.export.entity.ReportParam;
import com.rlyy.aibase.utils.base.TableNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 账号报表
 *
 * @author yuanqinglong
 * @since 2021/7/21 16:53
 */
@Service
public class AccountReportServiceImpl implements IReportService<ReportParam> {


    @Autowired
    private AccountReportDao accountReportDao;

    @Autowired
    private OperateLogDao operateLogDao;


    /**
     * 导出表
     *
     * @param reportParam 报告参数
     */
//    @Async
    @Override
    public void exportReportTable(ReportParam reportParam) {
        TableNode exportList = accountReportDao.accountReportExportList(reportParam);
        List<AccountReport> accountReports = JSON.parseArray(exportList.toJson(), AccountReport.class);
        accountReports.forEach(accountReport -> accountReport.processingAttributeValue(reportParam));
        // 保存导出日志 并上传到文件服务器
        this.saveLogCreateExcelUpload(accountReports, AccountReport.class, "账号报表", reportParam.getOperatorId());
    }

    /**
     * 导出详情列表
     *
     * @param reportParam 报告参数
     */
//    @Async
    @Override
    public void exportDetailTable(ReportParam reportParam) {
        TableNode detailListExport = accountReportDao.accountReportDetailListExport(reportParam);
        List<AccountReportDetail> accountReportDetails = JSON.parseArray(detailListExport.toJson(), AccountReportDetail.class);
        accountReportDetails.forEach(AccountReportDetail::processingAttributeValue);
        // 保存导出日志 并上传到文件服务器
        this.saveLogCreateExcelUpload(accountReportDetails, AccountReportDetail.class, "账号报表详情", reportParam.getOperatorId());
    }



    /**
     * 下载日志
     *
     * @param reportParam 报告参数
     * @param cycleIndex  循环次数
     * @return {@link List<DownloadLog>}
     */
    @Override
    public List<DownloadLog> downloadLog(ReportParam reportParam, int cycleIndex) {
        int offset = (cycleIndex - 1) * LIMIT_SIZES;
        TableNode accountLog = operateLogDao.accountLog(reportParam.getStartDate(), reportParam.getEndDate(), reportParam.getAutKeyid(), offset, LIMIT_SIZES);
        return JSON.parseArray(accountLog.toJson(), DownloadLog.class);
    }

    /**
     * 下载日志数
     *
     * @param reportParam 报告参数
     * @return int
     */
    @Override
    public int downloadLogCount(ReportParam reportParam) {
        return operateLogDao.accountLogCount(reportParam.getStartDate(), reportParam.getEndDate(), reportParam.getAutKeyid()).valueInt();
    }


}
